﻿using Common;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.FallsDropBed
{
    public partial class AddFallsDropNursing : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        public int Sum = 0;
        string Id = "";
        ElectronicSignature es;

        #endregion

        #region 窗体构造函数2个 内部和外部调用
        public AddFallsDropNursing(Tb_User loginUser, PatientInfo patientInfo, string Id)
        {
            InitializeComponent();
            //成员变量
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.Id = Id;
            es = new ElectronicSignature(LoginUser);//电签实例化        

            //绑定数据
            BindInfo(Id);
        }
        public AddFallsDropNursing(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;
            es = new ElectronicSignature(LoginUser);//电签实例化

            //绑定数据
            BindInfo(this.Id);
        }
        public AddFallsDropNursing(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = id;
            es = new ElectronicSignature(LoginUser);//电签实例化

            //绑定数据
            BindInfo(id);
        }

        /// <summary>
        /// 初始化加载印章图片
        /// </summary>
        private void ElectronicSignature()
        {
            ElectronicSignature es = new ElectronicSignature(LoginUser);
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, textzerenhushiqianming, picElectronicSignature, textzerenhushiqianming.Location);
        }


        #endregion

        #region 显示赋值
        public void BindInfo(string Id)
        {
            dateriqi.DateTime = DateTime.Now; //默认显示当前日期 
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            FallDown ps = new FallDown();
            ps = XmlUtil.XMLToObject<FallDown>(NursingFormData, Encoding.UTF8);
            dateriqi.DateTime = Convert.ToDateTime(ps.PingGuXiangMu.RiQi.ToString());//评估日期
            if (!string.IsNullOrEmpty(ps.ChuYuanRiQi))
                txtOUT_DATE.DateTime = Convert.ToDateTime(ps.ChuYuanRiQi);//出院日期
            textzongfen.Text = ps.PingGuXiangMu.ZongFen.ToString();//总分
            //年龄
            string NianLin = ps.PingGuXiangMu.NianLin;
            if (string.IsNullOrEmpty(NianLin)) NianLin = "0#0";
            if (NianLin.Equals("0")) NianLin = "1#0";
            if (NianLin.Equals("1")) NianLin = "0#1";
            if (NianLin.IndexOf('#') < 0) NianLin = "0#0";
            if (NianLin.Split('#').Length < 2) NianLin = "0#0";
            checkEditnianling1.Checked = NianLin.Split('#')[0].Equals("1");
            checkEditnianling2.Checked = NianLin.Split('#')[1].Equals("1");

            //既往史
            string[] jiwansghi = ps.PingGuXiangMu.JiWangShi.Split('#');
            if (jiwansghi[0] == "1")
                checkEditjiwangshi1.Checked = true;
            else
                checkEditjiwangshi1.Checked = false;

            if (jiwansghi[1] == "1")
                checkEditjiwangshi2.Checked = true;
            else
                checkEditjiwangshi2.Checked = false;
            //活动能力
            string[] huodongnengli = ps.PingGuXiangMu.HuoDongNengLi.Split('#');
            if (huodongnengli[0] == "1")
                checkEdithuodongnengli1.Checked = true;
            else
                checkEdithuodongnengli1.Checked = false;

            if (huodongnengli[1] == "1")
                checkEdithuodongnengli2.Checked = true;
            else
                checkEdithuodongnengli2.Checked = false;
            //意识状态控件由radio改为checkbox因为有历史数据所以选中用0，未选中用-1
            if (ps.PingGuXiangMu.YiShiZhuangTai == "0")
                checkEdityishizhangtai.Checked = true;
            else
                checkEdityishizhangtai.Checked = false;
            //感觉障碍
            if (ps.PingGuXiangMu.GanJueZhangAi == "0")
                checkEditganjuzhangai.Checked = true;
            else
                checkEditganjuzhangai.Checked = false;
            //用药情况
            string strYYQK = ps.PingGuXiangMu.YongYaoQingKuang;
            if (!string.IsNullOrEmpty(strYYQK))
            {
                if (strYYQK.Contains("#"))
                {
                    string[] yyqk = ps.PingGuXiangMu.YongYaoQingKuang.Split('#');
                    if (yyqk[0] == "1")
                        checkEdityongyaoqingkuang1.Checked = true;
                    else
                        checkEdityongyaoqingkuang1.Checked = false;

                    if (yyqk[1] == "1")
                        checkEdityongyaoqingkuang2.Checked = true;
                    else
                        checkEdityongyaoqingkuang2.Checked = false;
                }
                else
                {
                    if (strYYQK == "0")
                    {
                        checkEdityongyaoqingkuang1.Checked = true;
                    }
                    if (strYYQK == "1")
                    {
                        checkEdityongyaoqingkuang2.Checked = true;
                    }
                }
            }

            //排泄问题控件由radio改为checkbox因为有历史数据所以选中用0，未选中用-1
            if (ps.PingGuXiangMu.PaiXieWenTi == "0")
                checkEditpaixiewenti.Checked = true;
            else
                checkEditpaixiewenti.Checked = false;
            //治疗因素
            if (ps.PingGuXiangMu.ZhiLiaoYinSu == "0")
                checkEditzhiliaoyinsu.Checked = true;
            else
                checkEditzhiliaoyinsu.Checked = false;
            //护理措施
            var hlcs = ps.PingGuXiangMu.HuLiCuoShi.Split('#');
            if (hlcs[0] == "1")
                checkEdithulicuoshi1.Checked = true;
            else
                checkEdithulicuoshi1.Checked = false;
            if (hlcs[1] == "1")
                checkEdithulicuoshi2.Checked = true;
            else
                checkEdithulicuoshi2.Checked = false;
            if (hlcs[2] == "1")
                checkEdithulicuoshi3.Checked = true;
            else
                checkEdithulicuoshi3.Checked = false;
            if (hlcs[3] == "1")
                checkEdithulicuoshi4.Checked = true;
            else
                checkEdithulicuoshi4.Checked = false;
            if (hlcs[4] == "1")
                checkEdithulicuoshi5.Checked = true;
            else
                checkEdithulicuoshi5.Checked = false;
            if (hlcs[5] == "1")
                checkEdithulicuoshi6.Checked = true;
            else
                checkEdithulicuoshi6.Checked = false;
            if (hlcs[6] == "1")
                checkEdithulicuoshi7.Checked = true;
            else
                checkEdithulicuoshi7.Checked = false;
            if (hlcs[7] == "1")
                checkEdithulicuoshi8.Checked = true;
            else
                checkEdithulicuoshi8.Checked = false;
            if (hlcs[8] == "1")
                checkEdithulicuoshi9.Checked = true;
            else
                checkEdithulicuoshi9.Checked = false;
            if (hlcs[9] == "1")
                checkEdithulicuoshi10.Checked = true;
            else
                checkEdithulicuoshi10.Checked = false;
            if (hlcs[10] == "1")
                checkEdithulicuoshi11.Checked = true;
            else
                checkEdithulicuoshi11.Checked = false;
            if (hlcs[11] == "1")
                checkEdithulicuoshi12.Checked = true;
            else
                checkEdithulicuoshi12.Checked = false;
            textzerenhushiqianming.Text = ps.PingGuXiangMu.ZeRenHuShiQingZi;//责任护士签字
            hushizhangqianming.Text = ps.PingGuXiangMu.HuShiZhangQianZi;//护士长签字
            textEdithuanzhejiashuqianzi.Text = ps.HuanZheJiaShuQianZi;//患者家属签字
            comboBoxEdityhzgx.Text = ps.YuHuanZheGuanXi;//与患者关系
            //预防效果
            var yufangxiaoguo = ps.YuFangXiaoGuo.Split('#');
            if (yufangxiaoguo[0] == "1")
                checkEdityufangxiaoguo1.Checked = true;
            else
                checkEdityufangxiaoguo1.Checked = false;
            if (yufangxiaoguo[1] == "1")
                checkEdityufangxiaoguo2.Checked = true;
            else
                checkEdityufangxiaoguo2.Checked = false;
            //预防效果文本框
            textEdityfwbk.Text = ps.YuFangXiaoGuo_Text;

            //责任护士签字-电签
            es.SetSiginPic(LoginUser.DepartmentCode, ps.PingGuXiangMu.ZeRenHuShiQingZi_ID, picElectronicSignature, textzerenhushiqianming.Location, textzerenhushiqianming);
            if (textzerenhushiqianming.Text.Equals(this.LoginUser.UserName)) textzerenhushiqianmingId = true;
            //护士长签字-电签
            es.SetSiginPic(LoginUser.DepartmentCode, ps.PingGuXiangMu.HuShiZhangQianZi_ID, pictureBox1, hushizhangqianming.Location, hushizhangqianming);
            if (hushizhangqianming.Text.Equals(this.LoginUser.UserName)) textEdithuishizhangqianmingId = true;

        }
        #endregion

        #region 保存
        private void simpleButtonbaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateriqi.Text))
                dateriqi.DateTime = DateTime.Now;

            FallDown fdd = new FallDown();
            //日期
            fdd.PingGuXiangMu.RiQi = dateriqi.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //出院日期
            fdd.ChuYuanRiQi = txtOUT_DATE.Text;
            //年龄
            fdd.PingGuXiangMu.NianLin = (checkEditnianling1.Checked ? "1" : "0") + "#" + (checkEditnianling2.Checked ? "1" : "0");
            //既往史
            fdd.PingGuXiangMu.JiWangShi = (checkEditjiwangshi1.Checked ? "1" : "0") + "#" + (checkEditjiwangshi2.Checked ? "1" : "0");
            //活动能力
            fdd.PingGuXiangMu.HuoDongNengLi = (checkEdithuodongnengli1.Checked ? "1" : "0") + "#" + (checkEdithuodongnengli2.Checked ? "1" : "0");
            //意识状态控件由radio改为checkbox因为有历史数据所以选中用0，未选中用-1
            fdd.PingGuXiangMu.YiShiZhuangTai = checkEdityishizhangtai.Checked ? "0" : "-1";
            //感觉障碍
            fdd.PingGuXiangMu.GanJueZhangAi = checkEditganjuzhangai.Checked ? "0" : "-1";
            //用药情况
            fdd.PingGuXiangMu.YongYaoQingKuang = (checkEdityongyaoqingkuang1.Checked ? "1" : "0") + "#" + (checkEdityongyaoqingkuang2.Checked ? "1" : "0");
            //排泄问题
            fdd.PingGuXiangMu.PaiXieWenTi = checkEditpaixiewenti.Checked ? "0" : "-1";
            // 治疗因素
            fdd.PingGuXiangMu.ZhiLiaoYinSu = checkEditzhiliaoyinsu.Checked ? "0" : "-1";
            //护理措施
            fdd.PingGuXiangMu.HuLiCuoShi = (checkEdithulicuoshi1.Checked ? "1" : "0") + "#" + (checkEdithulicuoshi2.Checked ? "1" : "0") + "#" +
                                           (checkEdithulicuoshi3.Checked ? "1" : "0") + "#" + (checkEdithulicuoshi4.Checked ? "1" : "0") + "#" +
                                           (checkEdithulicuoshi5.Checked ? "1" : "0") + "#" + (checkEdithulicuoshi6.Checked ? "1" : "0") + "#" +
                                           (checkEdithulicuoshi7.Checked ? "1" : "0") + "#" + (checkEdithulicuoshi8.Checked ? "1" : "0") + "#" +
                                           (checkEdithulicuoshi9.Checked ? "1" : "0") + "#" + (checkEdithulicuoshi10.Checked ? "1" : "0") + "#" +
                                           (checkEdithulicuoshi11.Checked ? "1" : "0") + "#" + (checkEdithulicuoshi12.Checked ? "1" : "0");
            //总分
            fdd.PingGuXiangMu.ZongFen = textzongfen.Text;
            //责任护士签名
            fdd.PingGuXiangMu.ZeRenHuShiQingZi = textzerenhushiqianming.Text;
            if (textzerenhushiqianmingId && !string.IsNullOrEmpty(textzerenhushiqianming.Text) && textzerenhushiqianming.Text.Equals(this.LoginUser.UserName))
                fdd.PingGuXiangMu.ZeRenHuShiQingZi_ID = this.LoginUser.UserId;

            //护士长签名
            fdd.PingGuXiangMu.HuShiZhangQianZi = hushizhangqianming.Text;
            if (textEdithuishizhangqianmingId && !string.IsNullOrEmpty(hushizhangqianming.Text) && hushizhangqianming.Text.Equals(this.LoginUser.UserName))
                fdd.PingGuXiangMu.HuShiZhangQianZi_ID = this.LoginUser.UserId;

            //患者/家属签字
            fdd.HuanZheJiaShuQianZi = textEdithuanzhejiashuqianzi.Text;
            //与患者关系
            fdd.YuHuanZheGuanXi = comboBoxEdityhzgx.Text;
            //预防效果
            fdd.YuFangXiaoGuo = (checkEdityufangxiaoguo1.Checked ? "1" : "0") + "#" + (checkEdityufangxiaoguo2.Checked ? "1" : "0");
            //预防效果文本框
            fdd.YuFangXiaoGuo_Text = textEdityfwbk.Text;
            if (!checkEdityufangxiaoguo2.Checked)
                fdd.YuFangXiaoGuo_Text = string.Empty;

            //表单ID
            fdd.FormId = (int)FormType.FallDown;
            //表单名字
            fdd.FormName = EnumHelper.GetDescription(FormType.FallDown);
            //表单ID
            record.FormId = (int)FormType.FallDown;
            //表单名字
            record.FormName = EnumHelper.GetDescription(FormType.FallDown);
            //住院号
            record.PatientId = this.PatientInfo.PATIENT_ID;
            //住院次数
            record.VisitId = PatientInfo.VISIT_ID;
            //患者姓名
            record.PatientName = PatientInfo.PATIENT_NAME;
            //床号
            record.BedNo = PatientInfo.BED_NO;
            //当前患者科室ID
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;
            //当前患者科室名称
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;
            //当前患者病区ID
            record.AreaId = PatientInfo.NURSE_CELL_CODE;
            //当前患者病区名称
            record.AreaName = PatientInfo.NURSE_CELL_NAME;

            //时间
            record.AssessmentDate = dateriqi.DateTime;
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            if (string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(fdd.PingGuXiangMu.ZeRenHuShiQingZi))
            {
                fdd.PingGuXiangMu.ZeRenHuShiQingZi = this.LoginUser.UserName;
                fdd.PingGuXiangMu.ZeRenHuShiQingZi_ID = this.LoginUser.UserId;
            }

            record.NursingFormData = XmlUtil.ObjectToXML(fdd, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                if (Base.ESSwitch.Equals("on"))//电签开始
                {
                    string extraInfo = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><root><sign_sys>{0}</sign_sys><title>{1}</title><patient_id>{2}</patient_id><patient_name>{3}</patient_name><emr_id>{4}</emr_id><emr_catalog>{5}</emr_catalog><his_id>{6}</his_id><event_no>{7}</event_no><visit_number>{8}</visit_number><dept_name>{9}</dept_name><prescribe_number>{10}</prescribe_number><prescribe_sub_number>{11}</prescribe_sub_number><prescribe_operation>医嘱操作（发送、校对、停止、作废）或门诊处方操作（发送）</prescribe_operation><radiotherapy_id>放疗号</radiotherapy_id><process_name>CT定位申请单审核</process_name><process_time>CT定位申请单审核时间</process_time><extend_value_1>备用扩展1</extend_value_1><extend_value_2>备用扩展2</extend_value_2><extend_value_3>备用扩展3</extend_value_3><extend_value_4>备用扩展4</extend_value_4><extend_value_5>备用扩展5</extend_value_5><extend_value_6>备用扩展6</extend_value_6><extend_value_7>备用扩展7</extend_value_7><extend_value_8>备用扩展8</extend_value_8><extend_value_9>备用扩展9</extend_value_9></root>";
                    extraInfo = string.Format(extraInfo, EnumHelper.GetDescription(FormType.FallDown) + (record.Id > 0 ? "-更新" : "-新增"), EnumHelper.GetDescription(FormType.FallDown), record.PatientId, record.PatientName, record.Id, record.FormId, "", record.PatientId, record.VisitId, LoginUser.DepartmentName, record.PatientId, record.PatientId);
                    ElectronicSignature es = new ElectronicSignature(LoginUser);
                    bool res = es.GetElectronicSignatureImage(es, record.NursingFormData, extraInfo, record.FormId.ToString(), record.FormName, PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID);
                    if (!res) { MessageBox.Show("电子签名失败"); return; }
                }//电签结束                
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 取消
        private void simpleButtonquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region   计算总分
        public void SunTotal()
        {
            int Total = 0;
            int Total1 = 0;
            int Total2 = 0;
            int Total3 = 0;
            int Total4 = 0;
            int Total5 = 0;
            int Total6 = 0;
            int Total7 = 0;
            int Total8 = 0;
            int Total9 = 0;
            int Tota20 = 0;
            int Tota21 = 0;

            //年龄
            if (checkEditnianling1.Checked)
                Total = 2;
            else
                Total = 0;

            if (checkEditnianling2.Checked)
                Tota20 = 10;
            else
                Tota20 = 0;
            //既往史
            if (checkEditjiwangshi1.Checked)
                Total1 = 2;
            else
                Total1 = 0;

            if (checkEditjiwangshi2.Checked)
                Total8 = 1;
            else
                Total8 = 0;
            //活动能力
            if (checkEdithuodongnengli1.Checked)
                Total2 = 3;
            else
                Total2 = 0;

            if (checkEdithuodongnengli2.Checked)
                Total9 = 2;
            else
                Total9 = 0;
            //意识状态
            if (checkEdityishizhangtai.Checked)
                Total3 = 2;
            else
                Total3 = 0;
            //感觉障碍
            if (checkEditganjuzhangai.Checked)
                Total4 = 2;
            else
                Total4 = 0;
            //用药情况
            if (checkEdityongyaoqingkuang1.Checked)
                Total5 = 1;
            else
                Total5 = 0;

            if (checkEdityongyaoqingkuang2.Checked)
                Tota21 = 2;
            else
                Tota21 = 0;
            //排泄问题
            if (checkEditpaixiewenti.Checked)
                Total6 = 2;
            else
                Total6 = 0;
            //治疗因素
            if (checkEditzhiliaoyinsu.Checked)
                Total7 = 2;
            else
                Total7 = 0;

            Sum = Total + Total1 + Total2 + Total3 + Total4 + Total5 + Total6 + Total7 + Total8 + Total9 + Tota20 + Tota21;
            textzongfen.Text = Sum.ToString();
        }
        #endregion

        #region  总分
        //单选计算分值
        private void textzongfen_EditValueChanged(object sender, EventArgs e)
        {
            SunTotal();
        }
        //多选计算分值
        private void checkEditjiwangshi1_CheckedChanged(object sender, EventArgs e)
        {
            SunTotal();
        }
        #endregion

        private void checkEdityufangxiaoguo2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdityufangxiaoguo2.Checked)
            {
                textEdityfwbk.Visible = true;
            }
            else
            {
                textEdityfwbk.Visible = false;
            }
        }
        #region 文本框双击获取签名及按钮获取签名
        //责任护士签名
        bool textzerenhushiqianmingId = false;
        private void textzerenhushiqianming_DoubleClick(object sender, EventArgs e)
        {
            this.textzerenhushiqianming.Text = this.LoginUser.UserName;
            textzerenhushiqianmingId = true;
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, textzerenhushiqianming, picElectronicSignature, textzerenhushiqianming.Location);
        }

        //护士长签名
        bool textEdithuishizhangqianmingId = false;
        private void textEdithuishizhangqianming_DoubleClick(object sender, EventArgs e)
        {
            this.hushizhangqianming.Text = this.LoginUser.UserName;
            textEdithuishizhangqianmingId = true;
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, hushizhangqianming, pictureBox1, hushizhangqianming.Location);
        }
        //责任护士签名
        private void hushiqianming_Click(object sender, EventArgs e)
        {
            this.textzerenhushiqianming.Text = this.LoginUser.UserName;
            textzerenhushiqianmingId = true;
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, textzerenhushiqianming, picElectronicSignature, textzerenhushiqianming.Location);
        }
        //护士长签名
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.hushizhangqianming.Text = this.LoginUser.UserName;
            textEdithuishizhangqianmingId = true;
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, hushizhangqianming, pictureBox1, hushizhangqianming.Location);
        }
        #endregion
    }
}
